package org.naigu.peach.dal.dao.impl;

import guda.grape.autogen.common.BaseDAO;
import org.naigu.peach.dal.dao.UserDAO;
import org.naigu.peach.dal.dataobject.UserDO;
import guda.grape.autogen.common.page.BaseQuery;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDAOImpl extends BaseDAO implements UserDAO {

    @Override
	public Long insert(UserDO user) {
	      try {
                return (Long) getSqlMapperW().insert("user.insert",user);
          } catch (Exception e) {
                throw new RuntimeException(e);
          }

	}

    @Override
	public UserDO getUserById(Long userId) {
	    return (UserDO)getSqlMapClientTemplate().queryForObject("user.getUserById", userId);
	}

    @Override
	public boolean delUserById(Long userId) {
	    int num = 0;
        try {
            num = getSqlMapperW().delete("user.delUserById",userId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if(num>0){
            return true;
        }else{
            return false;
        }
	}

    @Override
	public List<UserDO> getUserByIds(List idsList) {
        Map m = new HashMap();
        m.put("idsList",idsList);
		return getSqlMapClientTemplate().queryForList("user.selectByIds", m);
	}

    @Override
	public void cleanAutoIncrement(Long userId) {
	    try {
            getSqlMapperW().update("user.cleanAutoIncrement",userId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
	}

    @Override
    public List<UserDO> selectForPage(BaseQuery baseQuery) {
        return getSqlMapClientTemplate().queryForList("user.selectForPage", baseQuery);
    }

    @Override
    public int countForPage(BaseQuery baseQuery) {
        return (Integer)getSqlMapClientTemplate().queryForObject("user.countForPage",baseQuery);
    }

    @Override
    public void updateByIdSelective(UserDO user) {
        try {
            getSqlMapperW().update("user.updateByIdSelective",user);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<UserDO> selectByEmail(String email) {
        return getSqlMapClientTemplate().queryForList("user.selectByEmail", email);
    }

    @Override
    public List<UserDO> selectByEmailAndPass(String email, String pass) {
        Map<String,String> param = new HashMap<String, String>();
        param.put("email",email);
        param.put("password",pass);
        return getSqlMapClientTemplate().queryForList("user.selectByEmailAndPass", param);
    }

    @Override
    public List<UserDO> selectByEmailAndRegCode(String email, String regCode) {
        Map<String,String> param = new HashMap<String, String>();
        param.put("email",email);
        param.put("regCode",regCode);
        return getSqlMapClientTemplate().queryForList("user.selectByEmailAndRegCode", param);
    }

}

